<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Set_pembayaran_tahunan extends CI_Controller {

	function __construct() {
        parent::__construct();
        $this->isLogin();
		$this->num_post=10;		
		$this->load->model("master/master_set_tahunan"); 
		$this->load->model("master/master_tapel"); 
		$this->load->model("master/master_lembaga"); 		
	}
	
	function isLogin(){
		$login=$this->session->userdata('isLogin');
		if(! isset($login)||$login != true){
			$this->session->set_flashdata('error_login', 'Untuk Mengakses Admin Anda Harus Login!');
			redirect('login');	
		}
	}
	
	function lembaga(){
		$data['list']=$this->master_lembaga->read();	
		$data['main_view']='master/set_pembayaran_tahunan/lembaga';
		$this->load->view('index',$data);
	}
	
	function index($id_lembaga,$offset = 0){
		
		$data['main_view']='master/set_pembayaran_tahunan/read';
		//$data['aksi_cari']='admin_client/cari_client';
		$uri_segment = 3;
		$num_post=$this->num_post;
		if($offset != 0){
			$offset = $this->uri->segment($uri_segment);
		}else{
			$offset=$offset;
		}
		$data['id_lembaga']=$id_lembaga;
		$data['hasil']=$this->master_set_tahunan->read($num_post, $offset,$id_lembaga);
		if(isset($data['hasil'])){
			foreach($data['hasil'] as $key=>$val){
				$data['hasil'][$key]['cek_bayar']=$this->master_set_tahunan->cek_pembayaran($val['id_tapel']);
			}
		}
		$data['offset']=$offset;
		$num_rows = $this->master_tapel->count_all_num_rows($id_lembaga);
			
		if($num_rows>0){
			// Membuat pagination
			$config['base_url'] = base_url().'set_pembayaran_tahunan/index';
			$config['cur_tag_open'] = '<li class=active><a href="#">';
			$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			
			$config['prev_link'] = '<<';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			
			$config['last_link'] = '>';
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			
			$config['first_link'] = '<';
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			
			$config['next_link'] = '>>';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			
			$config['total_rows'] = $num_rows;
			$config['per_page'] = $num_post;
			$config['uri_segment'] = $uri_segment;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
		}
		
		$this->load->view('index',$data);
	}
	
	function add($id_lembaga){
		$data['main_view']='master/set_pembayaran_tahunan/add';
		$data['id_lembaga']=$id_lembaga;
		$tapel=$this->master_tapel->tapel_semua();
		foreach($tapel as $val){
			$data['tapel'][$val['id_mr_th_ajaran']]=$val['th_ajaran'];
		}
		$this->load->view('index',$data);
	}
	
	function edit($id_lembaga,$id){
		$data['main_view']='master/set_pembayaran_tahunan/edit';
		$data['id_lembaga']=$id_lembaga;
		$data['detail_tapel']=$this->master_tapel->detail($id);
		$id_lembaga=$this->session->userdata('id_lembaga');
		$data['hasil']=$this->master_set_tahunan->get_detail($id,$id_lembaga);
		$this->load->view('index',$data);
	}
	
	function proses_add(){
		$id_tapel=$this->input->post('tapel');
		$id_lembaga=$this->input->post('id_lembaga');
		$id_komp=$this->input->post('id_komp');
		$nominal=$this->input->post('nominal');
		$jml=count($id_komp);
		for($i=0;$i<$jml;$i++){
			if($nominal[$i]!=""){
				$angka=str_replace(".","",$nominal[$i]);
				$angka=str_replace(",",".",$angka);
				$insert=$this->master_set_tahunan->insert_data($id_tapel,$id_komp[$i],$angka,$id_lembaga);
			}
		}  
		redirect('set_pembayaran_tahunan/index/'.$id_lembaga);
	}
	
	function detail(){
		$id_tapel=$this->input->post('id_tapel');
		$id_lembaga=$this->input->post('id_lembaga');
		$data['hasil']=$this->master_set_tahunan->get_detail($id_tapel,$id_lembaga);
		$this->load->view('master/set_pembayaran_tahunan/detail',$data);
	}
	
	function proses_edit(){
		$id_tapel=$this->input->post('id_tapel');
		$id_lembaga=$this->input->post('id_lembaga');
		$id_komp=$this->input->post('id_komp');
		$nominal=$this->input->post('nominal');
		$jml=count($id_komp);
		//hapus
		$hapus=$this->master_set_tahunan->del_by_tapel($id_tapel);
		for($i=0;$i<$jml;$i++){
			if($nominal[$i]!=""){
				$angka=str_replace(".","",$nominal[$i]);
				$angka=str_replace(",",".",$angka);
				$insert=$this->master_set_tahunan->insert_data($id_tapel,$id_komp[$i],$angka,$id_lembaga);
			}
		}  
		redirect('set_pembayaran_tahunan/index/'.$id_lembaga);
	}
	
	function delete($id_lembaga,$id){
		$hapus=$this->master_set_tahunan->del_by_tapel($id);
		redirect('set_pembayaran_tahunan/index/'.$id_lembaga);
	}
}
?>